﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Cemit.PolyServer
{
    /// <summary>
    /// 提供联级调用读取配置项的扩展
    /// </summary>
    public static class ConfigReadExtension
    {
        public static IConfigs Read(this IConfigs config, out string value, string key)
        {
            Read(config, out value, key, s => s);
            return config;
        }

        public static IConfigs Read<T>(this IConfigs config, out T value, string key, Func<string, T> converter)
        {
            value = converter(config.GetValue(key));
            return config;
        }

        public static IConfigs Read(this IConfigs config, out string[] values, string key)
        {
            Read(config, out values, key, s => s);
            return config;
        }

        public static IConfigs Read<T>(this IConfigs config, out T[] values, string key, Func<string, T> converter)
        {
            List<T> outs = new List<T>();
            foreach (var item in config.GetValues(key))
            {
                outs.Add(converter(item));
            }
            values = outs.ToArray();
            return config;
        }
    }
}
